package com.zhy.http.okhttp;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.builder.PostFileBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.builder.PostStringBuilder;
import com.zhy.http.okhttp.cookie.SimpleCookieJar;
import com.zhy.http.okhttp.https.HttpsUtils;
import com.zhy.http.okhttp.request.OkHttpRequest;
import com.zhy.http.okhttp.request.RequestCall;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import okhttp3.Call;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class OkHttpUtils
{
  public static final long DEFAULT_MILLISECONDS = 10000L;
  public static final String TAG = "OkHttpUtils";
  private static OkHttpUtils mInstance;
  private boolean debug;
  private Handler mDelivery;
  private OkHttpClient mOkHttpClient;
  private String tag;

  private OkHttpUtils()
  {
    OkHttpClient.Builder localBuilder = new OkHttpClient.Builder();
    localBuilder.cookieJar(new SimpleCookieJar());
    this.mDelivery = new Handler(Looper.getMainLooper());
    localBuilder.hostnameVerifier(new HostnameVerifier()
    {
      public boolean verify(String paramAnonymousString, SSLSession paramAnonymousSSLSession)
      {
        return true;
      }
    });
    this.mOkHttpClient = localBuilder.build();
  }

  public static GetBuilder get()
  {
    return new GetBuilder();
  }

  public static OkHttpUtils getInstance()
  {
    if (mInstance == null);
    try
    {
      if (mInstance == null)
        mInstance = new OkHttpUtils();
      return mInstance;
    }
    finally
    {
    }
  }

  public static PostFormBuilder post()
  {
    return new PostFormBuilder();
  }

  public static PostFileBuilder postFile()
  {
    return new PostFileBuilder();
  }

  public static PostStringBuilder postString()
  {
    return new PostStringBuilder();
  }

  public void cancelTag(Object paramObject)
  {
    Iterator localIterator1 = this.mOkHttpClient.dispatcher().queuedCalls().iterator();
    Iterator localIterator2;
    if (!localIterator1.hasNext())
      localIterator2 = this.mOkHttpClient.dispatcher().runningCalls().iterator();
    while (true)
    {
      if (!localIterator2.hasNext())
      {
        return;
        Call localCall1 = (Call)localIterator1.next();
        if (!paramObject.equals(localCall1.request().tag()))
          break;
        localCall1.cancel();
        break;
      }
      Call localCall2 = (Call)localIterator2.next();
      if (paramObject.equals(localCall2.request().tag()))
        localCall2.cancel();
    }
  }

  public OkHttpUtils debug(String paramString)
  {
    this.debug = true;
    this.tag = paramString;
    return this;
  }

  public void execute(RequestCall paramRequestCall, com.zhy.http.okhttp.callback.Callback paramCallback)
  {
    if (this.debug)
    {
      if (TextUtils.isEmpty(this.tag))
        this.tag = "OkHttpUtils";
      Log.d(this.tag, "{method:" + paramRequestCall.getRequest().method() + ", detail:" + paramRequestCall.getOkHttpRequest().toString() + "}");
    }
    if (paramCallback == null)
      paramCallback = com.zhy.http.okhttp.callback.Callback.CALLBACK_DEFAULT;
    final com.zhy.http.okhttp.callback.Callback localCallback = paramCallback;
    final Call localCall = paramRequestCall.getCall();
    localCall.enqueue(new okhttp3.Callback()
    {
      public void onFailure(Request paramAnonymousRequest, IOException paramAnonymousIOException)
      {
        OkHttpUtils.this.sendFailResultCallback(localCall, paramAnonymousIOException, localCallback);
      }

      public void onResponse(Response paramAnonymousResponse)
        throws IOException
      {
        if ((paramAnonymousResponse.code() >= 400) && (paramAnonymousResponse.code() <= 599))
          try
          {
            OkHttpUtils.this.sendFailResultCallback(localCall, new RuntimeException(paramAnonymousResponse.body().string()), localCallback);
            return;
          }
          catch (IOException localIOException)
          {
            localIOException.printStackTrace();
            return;
          }
        try
        {
          Object localObject = localCallback.parseNetworkResponse(paramAnonymousResponse);
          OkHttpUtils.this.sendSuccessResultCallback(localObject, localCallback);
          return;
        }
        catch (Exception localException)
        {
          OkHttpUtils.this.sendFailResultCallback(localCall, localException, localCallback);
        }
      }
    });
  }

  public Handler getDelivery()
  {
    return this.mDelivery;
  }

  public OkHttpClient getOkHttpClient()
  {
    return this.mOkHttpClient;
  }

  public void sendFailResultCallback(final Call paramCall, final Exception paramException, final com.zhy.http.okhttp.callback.Callback paramCallback)
  {
    if (paramCallback == null)
      return;
    this.mDelivery.post(new Runnable()
    {
      public void run()
      {
        paramCallback.onError(paramCall, paramException);
        paramCallback.onAfter();
      }
    });
  }

  public void sendSuccessResultCallback(final Object paramObject, final com.zhy.http.okhttp.callback.Callback paramCallback)
  {
    if (paramCallback == null)
      return;
    this.mDelivery.post(new Runnable()
    {
      public void run()
      {
        paramCallback.onResponse(paramObject);
        paramCallback.onAfter();
      }
    });
  }

  public void setCertificates(InputStream[] paramArrayOfInputStream)
  {
    this.mOkHttpClient = getOkHttpClient().newBuilder().sslSocketFactory(HttpsUtils.getSslSocketFactory(paramArrayOfInputStream, null, null)).build();
  }

  public void setConnectTimeout(int paramInt, TimeUnit paramTimeUnit)
  {
    this.mOkHttpClient = getOkHttpClient().newBuilder().connectTimeout(paramInt, paramTimeUnit).build();
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.zhy.http.okhttp.OkHttpUtils
 * JD-Core Version:    0.6.2
 */